/*
* Copyright (c) 2008, Vladimir Gumenuk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the copyright holder nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "StdAfx.h"
#include "GNX4TblFile.h"
#include "resource.h"

CGNX4TblFile::CGNX4TblFile()
{
    ASSERT( sizeof(GNX4Preset) == 2684 );
    //ZeroMemory( &m_Hdr, sizeof(m_Hdr) );
    //ZeroMemory( &m_Presets, sizeof(m_Presets) );
    TRACE( "size: %d\n", sizeof(GNX4Preset) );

    //for( DWORD i = 0; i < GNX_MAX_PRESETS; i++ )
    //{
    //    wcscpy_s( m_Presets[i].wszName, L"EMPTY     " );
    //}

    HRSRC hSrc = FindResourceA( NULL, MAKEINTRESOURCEA(IDR_BIN1), "BIN" );
    if( hSrc )
    {
        DWORD dwSize = SizeofResource( NULL, hSrc );
        if( dwSize == ( sizeof(m_Hdr) + sizeof(m_Presets) ) )
        {
            HGLOBAL hGlb = LoadResource( NULL, hSrc );
            if( hGlb )
            {
                LPVOID lpBin = LockResource( hGlb );
                if( lpBin )
                {
                    memcpy( &m_Hdr, lpBin, sizeof(m_Hdr) );
                    memcpy( &m_Presets, (LPBYTE)lpBin+sizeof(m_Hdr), sizeof(m_Presets) );
                    UnlockResource( lpBin );
                }

                FreeResource( hGlb );
            }
        }
    }
}

CGNX4TblFile::~CGNX4TblFile()
{
}

BOOL CGNX4TblFile::Load( LPCSTR lpszFileName )
{
    if( !lpszFileName )
        return FALSE;

    BOOL bRet = FALSE;

    CFile file;
    TRY
    {
        if( file.Open( lpszFileName, CFile::modeRead|CFile::shareDenyNone|CFile::normal ) )
        {
            if( file.Read( &m_Hdr, sizeof(m_Hdr) ) == sizeof(m_Hdr) )
            {
                if( file.Read( &m_Presets, sizeof(m_Presets) ) == sizeof(m_Presets) )
                {
                    int iRet = IDYES;

                    if( m_Hdr.wVersion != 3 )
                        iRet = AfxMessageBox( _T("This version of the tbl file is unknown. Load anyway?"), MB_YESNOCANCEL );

                    if( iRet == IDYES )
                    {
                        bRet = TRUE;
                    } 
                }
            }

            file.Close();
        }
    }
    CATCH (CFileException, e)
    {
        char szError[MAX_PATH] = {0};
        e->GetErrorMessage( &szError[0], MAX_PATH );
        AfxMessageBox( szError );
        e->Delete();
    }
    END_CATCH

    return bRet;
}

BOOL CGNX4TblFile::Save( LPCSTR lpszFileName )
{
    if( !lpszFileName )
        return FALSE;

    BOOL bRet = FALSE;

    CFile file;
    TRY
    {
        if( file.Open( lpszFileName, CFile::modeCreate|CFile::modeWrite ) )
        {
            file.Write( &m_Hdr, sizeof(m_Hdr) );
            file.Write( &m_Presets, sizeof(m_Presets) );
            file.Close();
            bRet = TRUE;
        }
    }
    CATCH( CFileException, e )
    {
        char szError[MAX_PATH] = {0};
        e->GetErrorMessage( &szError[0], MAX_PATH );
        AfxMessageBox( szError );
        e->Delete();
    }
    END_CATCH

    return bRet;
}

BOOL CGNX4TblFile::MovePresetUp( UINT iIdx )
{
    if( iIdx == 0 || iIdx >= GNX_MAX_PRESETS )
        return FALSE;

    PRESET temp;

    memcpy( &temp, &m_Presets[iIdx], sizeof(PRESET) );
    memcpy( &m_Presets[iIdx], &m_Presets[iIdx-1],  sizeof(PRESET) );
    memcpy( &m_Presets[iIdx-1], &temp,  sizeof(PRESET) );

    return TRUE;
}

BOOL CGNX4TblFile::MovePresetDown( UINT iIdx )
{
    if( iIdx == GNX_MAX_PRESETS-1 || iIdx >= GNX_MAX_PRESETS )
        return FALSE;

    PRESET temp;

    memcpy( &temp, &m_Presets[iIdx], sizeof(PRESET) );
    memcpy( &m_Presets[iIdx], &m_Presets[iIdx+1],  sizeof(PRESET) );
    memcpy( &m_Presets[iIdx+1], &temp,  sizeof(PRESET) );

    return TRUE;
}

